package com.spider.admin.service.apimarks;

import com.spider.admin.exception.SpiderException;
import com.spider.admin.service.accountPerm.AccountServicePermService;
import com.spider.admin.store.primary.mapper.TenantAppTreeMapper;
import com.spider.admin.store.primary.model.AccountServicePerm;
import com.spider.admin.store.primary.model.ServiceInfo;
import com.spider.admin.store.primary.model.TenantAndAppInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class ApiMarksServiceImpl implements ApiMarksService {

    @Autowired
    private TenantAppTreeMapper tenantAppTreeMapper;



    @Autowired
    private AccountServicePermService accountServicePermService;

    @Override
    public List<TenantAndAppInfo> getAll(String tenantId) throws SpiderException {
        return tenantAppTreeMapper.getAllInfo(tenantId);
    }

    @Override
    public List<ServiceInfo> getServiceList(String gId, String account, String type) throws SpiderException {
        List<ServiceInfo> result = null;
        if (StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(gId)) {
            if (type.equals("app")) {
                result = tenantAppTreeMapper.getServiceList(account,gId, null);
            } else {
                result = tenantAppTreeMapper.getServiceList(account,null, gId);
            }
        } else {
            result = tenantAppTreeMapper.getServiceList(account,null, null);
        }
        return result;
    }

    @Override
    public boolean subjectService(AccountServicePerm accountServicePerm) throws SpiderException {
        int count = accountServicePermService.saveAccountServicePerm(accountServicePerm);

        return count > 0 ? true : false;
    }

    @Override
    public boolean unSubjectService(String serviceId, String account) throws SpiderException {
        int count = accountServicePermService.deleteAccountServicePerm(account, serviceId);
        return count > 0 ? true : false;
    }

    @Override
    public List<ServiceInfo> getAccountServiceList(String gId, String account, String type) {
        List<ServiceInfo> result = null;
        if (StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(gId)) {
            if (type.equals("app")) {
                result = tenantAppTreeMapper.getAccountServiceList(account,gId, null);
            } else {
                result = tenantAppTreeMapper.getAccountServiceList(account,null, gId);
            }
        } else {
            result = tenantAppTreeMapper.getAccountServiceList(account,null, null);
        }
        return result;
    }
}
